import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
// import { getGrid, isMobileApp, isPadApp, isPcApp } from '@/utils';

// 获取栅格
// const grid = getGrid();

// 异步actions
// 更新store中用户的信息，可以用来更新用户store中的权限信息
// export const updateUserInfo = createAsyncThunk('main/updateUserInfo', (status=true) => validate());
export const updateUserInfo = createAsyncThunk(
    'main/updateUserInfo',
    async (params) => {
        try {
            const res = await getAppoint(params);
            if (res.code == 0) {
                const newObj = res.events || {}
                return newObj
            } else {
                throw new Error(res.msg);
            }
        } catch (error) {
            throw new Error('Failed to fetch list. Please try again later.');
        }
    }
);
// main store
const initialState = {
    userInfo: {},
    userError:null,
    unReadNum:0,
    // grid,
    // isMobile: isMobileApp(grid),
    // isPad: isPadApp(grid),
    // isPc: isPcApp(grid),
    showLoginModal: false,
};

const main = createSlice({
    name: 'main',
    initialState,
    reducers: {
        setUserInfo: (state, action) => { state.userInfo = action.payload },
        setUnReadNum: (state, action) => { state.unReadNum = action.payload },
        setLoginOpen: (state, action) => { state.showLoginModal = action.payload },
    },
    extraReducers: (builder) => {
        builder.addCase(updateUserInfo.fulfilled, (state, action) => {
            let response = action.payload || {};
            if (response?.code=== 0) {
                state.userInfo = response?.data || {};
            }
        });
        builder.addCase(updateUserInfo.rejected, (state, action) => {
            state.userError = action.error.message || null;
        });
    },
});

// 同步actions
export const { setUserInfo,setLoginOpen,setUnReadNum } = main.actions;
export default main.reducer;